package cn.sfrog.project.biz.web.controller;

import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.domain.BizMessageConversation;
import cn.sfrog.project.biz.admin.domain.vo.BizMessageLogQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizMessageLogVO;
import cn.sfrog.project.biz.admin.service.IBizMessageConversationService;
import cn.sfrog.project.biz.admin.service.IBizMessageLogService;
import cn.sfrog.project.biz.web.vo.websocket.AppConversationQueryVO;
import cn.sfrog.project.biz.web.vo.websocket.AppConversationResultVO;
import cn.sfrog.project.biz.web.vo.websocket.AppKfUserVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/message")
@Api(value = "/api/message", tags = "APP用户聊天相关")
public class AppMessageController extends BaseController {

    @Autowired
    private IBizMessageLogService bizMessageLogService;
    @Autowired
    private IBizMessageConversationService bizMessageConversationService;

    @ApiOperation(value = "获取聊天会话列表")
    @GetMapping("/getConversationList")
    public TableDataInfo<List<AppConversationResultVO>> getConversationList(AppConversationQueryVO query){
        query.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        startPage();
        List<AppConversationResultVO> conversationList =  bizMessageConversationService.selectAppConversationList(query);
        return getDataTable(conversationList);
    }



    @ApiOperation(value = "获取聊天消息记录列表")
    @GetMapping("/getMessageLogList")
    public TableDataInfo<BizMessageLogVO> getMessageLogList(@Validated  BizMessageLogQueryVO query){
        Page<BizMessageLogVO> page = PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<BizMessageLogVO> bizMessageLogVOS = bizMessageLogService.selectBizMessageLogList(query);
        TableDataInfo<BizMessageLogVO> dataTable = getDataTable(page.getResult());
        dataTable.setLastPage(query.getPageSize() > page.getResult().size());
        return dataTable;
    }


    @ApiOperation(value = "设置消息已读")
    @PostMapping("/setMessageRead/{messageId}")
    public ApiAjaxResult setMessageRead(@PathVariable Long messageId){
        Boolean flag =  bizMessageLogService.setMessageRead(messageId);
        return ApiAjaxResult.success(flag);
    }

    @ApiOperation(value = "获取未读消息数量")
    @GetMapping("/getUnReadCount")
    public ApiAjaxResult getUnReadCount(){
        Integer count = bizMessageConversationService.selectUnReadCount();
        return ApiAjaxResult.success(count);
    }

    @ApiOperation(value = "根据会话id设置消息已读")
    @PostMapping("/setMessageRead/{conversationId}")
    public ApiAjaxResult setMessageReadByConversationId(@PathVariable Long conversationId){
        return ApiAjaxResult.success( bizMessageLogService.setMessageReadByConversationId(conversationId));
    }


    @ApiOperation(value = "获取用户端聊天消息记录列表")
    @GetMapping("/getMessageLogListByUser")
    public TableDataInfo<BizMessageLogVO> getMessageLogListByUser(@Validated  BizMessageLogQueryVO query){
        //首先根据发送人和接收人id查询到会话
        Long currentUserId = SecurityUtils.getLoginUser().getUser().getUserId();
        LambdaQueryWrapper<BizMessageConversation> queryWrapper = Wrappers.lambdaQuery(BizMessageConversation.class)
                .or(e -> e.eq(BizMessageConversation::getSendUserId,query.getSendUserId()).eq(BizMessageConversation::getReceiveUserId,currentUserId))
                .or(e -> e.eq(BizMessageConversation::getSendUserId,currentUserId).eq(BizMessageConversation::getReceiveUserId,query.getSendUserId()))
                .or(e -> e.eq(BizMessageConversation::getSendUserId,currentUserId).eq(BizMessageConversation::getReceiveUserId,query.getReceiveUserId()))
                .or(e -> e.eq(BizMessageConversation::getSendUserId,query.getReceiveUserId()).eq(BizMessageConversation::getReceiveUserId,currentUserId));
        BizMessageConversation bizMessageConversation = BizMessageConversation.DAO.selectOne(queryWrapper);
        BizMessageLogQueryVO queryVO = new BizMessageLogQueryVO();
        queryVO.setMessageId(query.getMessageId());
        if(bizMessageConversation !=null){
            queryVO.setConversationId(bizMessageConversation.getId());
        }else {
            queryVO.setConversationId(0L);
        }
        Page<BizMessageLogVO> page = PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<BizMessageLogVO> bizMessageLogVOS = bizMessageLogService.selectBizMessageLogList(queryVO);
        TableDataInfo<BizMessageLogVO> dataTable = getDataTable(page.getResult());
        dataTable.setLastPage(query.getPageSize() > page.getResult().size());
        return dataTable;
    }

    @ApiOperation(value = "获取聊天客服")
    @GetMapping("/getKfUser")
    public ApiAjaxResult<AppKfUserVO> getKfUser(){
      AppKfUserVO appKfUserVO =  bizMessageConversationService.selectKfUser();
      return ApiAjaxResult.success(appKfUserVO);
    }

}
